﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core.Data;

namespace AG.Editor.MapUI.Controls
{
    public partial class AGEMapPanel : UserControl
    {
        float _originOffsetX;

        float TW = 64.0f;
        float TH = 47.0f;
        AGMap _map;

        public AGEMapPanel()
        {
            InitializeComponent();
        }

        public void SetMap(AGMap map)
        {
            _map = map;
            this.Width = (int)((_map.Col + _map.Row) * TW / 2);
            this.Height = (int)((_map.Row + _map.Col) * TH / 2);
            _originOffsetX = _map.Col * TH / 2;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        
            // render
            if (_map == null)
            {
                return;
            }

            for (int rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (int colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetX;
                    e.Graphics.DrawLine(Pens.Green, pt.X, pt.Y, pt.X + TW / 2, pt.Y - TH / 2);
                    e.Graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW, pt.Y);
                    e.Graphics.DrawLine(Pens.Green, pt.X + TW, pt.Y, pt.X + TW / 2, pt.Y + TH / 2);
                    e.Graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y + TH / 2, pt.X, pt.Y);
                }
            }
        }

        private PointF MapPosToMapPoint(int row, int col)
        {
            float x = TW / 2 * (row + col);
            float y = TH / 2 * (row - col);
            return new PointF(x, y);
        }
    }
}
